//
//  CBPickerView.swift
//  CBPickerViewDemo
//
//  Created by Creater on 2018/9/19.
//  Copyright © 2018年 Creater. All rights reserved.
//

import UIKit


let SCREEN_SIZE = UIScreen.main.bounds.size
let SCREEN_BOUNDS = CGRect(origin: CGPoint(x: 0, y: 0), size: SCREEN_SIZE)
typealias PickerConfirmCallback = (_ selectedIdx:Int) -> Void

class CBPickerView: UIView, UIPickerViewDelegate, UIPickerViewDataSource {
    
    var darkShadowView: UIView
    var contentView: UIView
    var confirmButton: UIButton
    var cancelButton: UIButton
    var pickerView: UIPickerView
    var pickerConfirmCallback: PickerConfirmCallback?
    var titleArray: Array<String>
    
    override func draw(_ rect: CGRect) {
        // Drawing code
        
    }
    
    public static func showInView(view: UIView?, itemsArr: Array<String>, selectedHandlers: @escaping PickerConfirmCallback) {
        if view == nil {
            return
        }
        let pickerView = CBPickerView.init(superView: view!)
        pickerView.titleArray = itemsArr
        pickerView.pickerConfirmCallback = selectedHandlers
        pickerView.pickerView.reloadComponent(0)
        pickerView.showSheet()

    }
    
    init(superView: UIView) {
        self.contentView = UIView.init(frame: CGRect(x: 0, y: SCREEN_SIZE.height, width: SCREEN_SIZE.width, height: CBUniversalTool.pickerHeight()))
        self.darkShadowView = UIView.init(frame: SCREEN_BOUNDS)
        self.cancelButton = UIButton.init(frame: CGRect(x: 0, y: 0, width: 80, height: 40))
        self.confirmButton = UIButton.init(frame: CGRect(x: SCREEN_SIZE.width - 80.0, y: 0, width: 80.0, height: 40.0))
        self.pickerView = UIPickerView.init(frame: CGRect(x: 0, y: 20, width: SCREEN_SIZE.width, height: CBUniversalTool.pickerHeight() - CBUniversalTool.bottomMargin()))
        self.titleArray = [];
        super.init(frame: SCREEN_BOUNDS)
        self.addSubview(contentView)
        self.frame = SCREEN_BOUNDS
        self.darkShadowView.alpha = 0.0
        self.addSubview(self.darkShadowView)
        let tap = UIGestureRecognizer.init(target: self, action: #selector(hideSheet))
        self.darkShadowView.addGestureRecognizer(tap)
        superView.window?.addSubview(self)
        let headLine = UIView()
        headLine.backgroundColor = UIColor(red:202/255.0, green:202/255.0, blue:202/255.0, alpha: 1)
        headLine.frame = CGRect(x: 0, y: 0, width: SCREEN_SIZE.width, height: 0.5)
        self.contentView.addSubview(headLine)
        cancelButton.setTitle("取消", for: .normal)
        cancelButton.setTitleColor(.blue, for: .normal)
        cancelButton.addTarget(self, action: #selector(CBPickerView.cancelEvent), for: .touchUpInside)
        confirmButton.setTitle("确定", for: .normal)
        confirmButton.setTitleColor(.blue, for: .normal)
        confirmButton.addTarget(self, action: #selector(CBPickerView.confirmEvent), for: .touchUpInside)
        pickerView.dataSource = self
        pickerView.delegate = self
        pickerView.showsSelectionIndicator = true
        
        self.contentView.addSubview(pickerView)
        self.contentView.addSubview(confirmButton)
        self.contentView.addSubview(cancelButton)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func showSheet() {
        UIView.animate(withDuration: 0.3) {
            var frame = self.contentView.frame
            frame.origin.y = self.frame.size.height - self.contentView.frame.size.height
            self.contentView.frame = frame
            var shadowViewFrame = self.darkShadowView.frame
            shadowViewFrame.size.height = self.frame.size.height - self.contentView.frame.size.height
            self.darkShadowView.frame = shadowViewFrame
            self.darkShadowView.alpha = 0.5
        }
    }
    
    @objc func hideSheet() {
        UIView.animate(withDuration: 0.3, animations: {
            var frame = self.contentView.frame
            frame.origin.y = self.frame.size.height
            self.contentView.frame = frame
            self.darkShadowView.alpha = 0
            var shadowFrame = self.darkShadowView.frame
            shadowFrame.size.height = self.frame.size.height
            self.darkShadowView.frame = shadowFrame
            
        }) { (finished) in
            self.removeFromSuperview()
        }
    }
    
    @objc func cancelEvent() {
        self.hideSheet()
    }
    
    @objc func confirmEvent() {
        let idx = self.pickerView.selectedRow(inComponent: 0)
        if self.pickerConfirmCallback != nil {
            self.pickerConfirmCallback!(idx)
        }
        self.hideSheet()
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return self.titleArray.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return self.titleArray[row]
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        var pickerLabel = view as? UILabel
        if pickerLabel == nil {
            pickerLabel = UILabel()
            pickerLabel!.adjustsFontSizeToFitWidth = true
            pickerLabel!.textAlignment = .center
            pickerLabel!.backgroundColor = .clear
            pickerLabel!.text = self.pickerView(pickerView, titleForRow: row, forComponent: component)
        }
        for subView in pickerView.subviews {
            if subView.bounds.size.height < 0.6 {
                subView.backgroundColor = UIColor.init(white: 0, alpha: 0.2)
            }
        }
        return pickerLabel!
    }
}
